package com.caifeng.goodstwo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class Shop extends AppCompatActivity implements View.OnClickListener{
    ListView listView;
    List<Goods> list;
    MyHelp myHelp;
    MyAdapter adapter;
    ImageView back;
    String TAG = "Shop";
    TextView sumprice;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
        init();


    }

    private void init() {
        listView = findViewById(R.id.l1);
        back = findViewById(R.id.back);//返回
        myHelp = new MyHelp(this);
        sumprice=findViewById(R.id.sumPrice);

        if (myHelp.getSum()!=0){
            sumprice.setText("￥"+myHelp.getSum());
        }else {
            sumprice.setText("￥0");
        }

        showQueryData();

        back.setOnClickListener(this);
//        listView.setOnItemClickListener(this);

    }

    //显示所有数据
    private void showQueryData() {
        if (list != null) {
            list.clear();
        }

        list = myHelp.query();

        sumprice.setText("￥"+myHelp.getSum());
        adapter = new MyAdapter(list, this);
        listView.setAdapter(adapter);

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            //返回
            case R.id.back:
                Intent intent2 = new Intent(Shop.this, MainActivity.class);
                startActivity(intent2);
                break;
            case R.id.jian:
                final int position = (int) view.getTag();
                Goods goods = (Goods) list.get(position);
                String id = goods.getId();
                String name = goods.getName();
                String number = String.valueOf(myHelp.getNum(id));
                if (Integer.parseInt(number) == 1) {
                    //就要执行删除语句
                    showToast("您删除了" + name + "商品");
                    myHelp.deleteGoods(id);
                    showQueryData();
                } else {
                    showToast("减少一件！");
                    myHelp.updateData(id, "minus");
                    showQueryData();
                }

                break;
            case R.id.jia:
                final int positionAdd = (int) view.getTag();
                Goods goodsAdd = (Goods) list.get(positionAdd);
                String idAdd = goodsAdd.getId();
                showToast("再加一件！");
                myHelp.updateData(idAdd, "add");
//                myHelp.updateData(idAdd,"add");

                showQueryData();
                break;
        }
    }

    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

}
